﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace RobotControl
{
    public class PComm
    {        

        [DllImport("PComm.dll", EntryPoint = "sio_open")]
        public static extern int sio_open(int port);

        [DllImport("PComm.dll", EntryPoint = "sio_close")]
        public static extern int sio_close(int port);

        [DllImport("PComm.dll", EntryPoint = "sio_ioctl")]
        public static extern int sio_ioctl(int port, int baud, int mode);        

        [DllImport("PComm.dll", EntryPoint = "sio_getch")]
        public static extern int sio_getch(int port);

        [DllImport("PComm.dll", EntryPoint = "sio_read")]
        public static extern int sio_read(int port, ref char[] buf, int len);

        [DllImport("PComm.dll", EntryPoint = "sio_putch")]
        public static extern int sio_putch(int port, int term);

        [DllImport("PComm.dll", EntryPoint = "sio_write")]
        public static extern int sio_write(int port, ref char[] buf, int len);

        [DllImport("PComm.dll", EntryPoint = "sio_flush")]
        public static extern int sio_flush(int port, int func);

        [DllImport("PComm.dll", EntryPoint = "sio_iqueue")]
        public static extern long sio_iqueue(int port);

        [DllImport("PComm.dll", EntryPoint = "sio_oqueue")]
        public static extern long sio_oqueue(int port);

        [DllImport("PComm.dll", EntryPoint = "sio_lstatus")]
        public static extern int sio_lstatus(int port);

        [DllImport("PComm.dll", EntryPoint = "sio_lctrl")]
        public static extern int sio_lctrl(int port, int mode);

        public delegate void delegateOnCntIrq(int port);
        [DllImport("PComm.dll", EntryPoint = "sio_cnt_irq")]
        public static extern int sio_cnt_irq(int port, delegateOnCntIrq OnCntIrq, int count);

        public delegate void delegateOnModemIrq(int port);
        [DllImport("PComm.dll", EntryPoint = "sio_modem_irq")]
        public static extern int sio_modem_irq(int port, delegateOnModemIrq OnModemIrq);

        public delegate void delegateOnBreakIrq(int port);
        [DllImport("PComm.dll", EntryPoint = "sio_break_irq")]
        public static extern int sio_break_irq(int port, delegateOnBreakIrq OnBreakIrq);

        public delegate void delegateOnTxEmptyIrq(int port);
        [DllImport("PComm.dll", EntryPoint = "sio_Tx_empty_irq")]
        public static extern int sio_Tx_empty_irq(int port, delegateOnTxEmptyIrq OnTxEmptyIrq);

        [DllImport("PComm.dll", EntryPoint = "sio_break")]
        public static extern int sio_break(int port, int time);

        [DllImport("PComm.dll", EntryPoint = "sio_break_ex")]
        public static extern int sio_break_ex(int port, int time);

        [DllImport("PComm.dll", EntryPoint = "sio_flowctrl")]
        public static extern int sio_flowctrl(int port, int mode);

        [DllImport("PComm.dll", EntryPoint = "sio_Tx_hold")]
        public static extern int sio_Tx_hold(int port);


        [DllImport("PComm.dll", EntryPoint = "sio_getbaud")]
        public static extern long sio_getbaud(int port);

        [DllImport("PComm.dll", EntryPoint = "sio_getmode")]
        public static extern int sio_getmode(int port);

        [DllImport("PComm.dll", EntryPoint = "sio_getflow")]
        public static extern int sio_getflow(int port);

        [DllImport("PComm.dll", EntryPoint = "sio_DTR")]
        public static extern int sio_DTR(int port, int mode);

        [DllImport("PComm.dll", EntryPoint = "sio_RTS")]
        public static extern int sio_RTS(int port, int mode);

        [DllImport("PComm.dll", EntryPoint = "sio_baud")]
        public static extern int sio_baud(int port, long speed);

        [DllImport("PComm.dll", EntryPoint = "sio_data_status")]
        public static extern int sio_data_status(int port);

        public delegate void delegateOnTermIrq(int port);
        [DllImport("PComm.dll", EntryPoint = "sio_term_irq")]
        public static extern int sio_term_irq(int port, delegateOnTermIrq OnTermIrq, char code);

        [DllImport("PComm.dll", EntryPoint = "sio_linput")]
        public static extern int sio_linput(int port, ref byte[] buf, int lne, int term);

        [DllImport("PComm.dll", EntryPoint = "sio_putb_x")]
        public static extern int sio_putb_x(int port, ref byte[] buf, int len, int tick);

        [DllImport("PComm.dll", EntryPoint = "sio_putb_x_ex")]
        public static extern int sio_putb_x_ex(int port, ref byte[] buf, int len, int tms);

        [DllImport("PComm.dll", EntryPoint = "sio_view")]
        public static extern int sio_view(int port, ref byte[] buf, int len);

        [DllImport("PComm.dll", EntryPoint = "sio_TxLowWater")]
        public static extern int sio_TxLowWater(int port, int size);

        [DllImport("PComm.dll", EntryPoint = "sio_AbortWrite")]
        public static extern int sio_AbortWrite(int port);

        [DllImport("PComm.dll", EntryPoint = "sio_SetWriteTimeouts")]
        public static extern int sio_SetWriteTimeouts(int port, uint TotalTimeouts);

        [DllImport("PComm.dll", EntryPoint = "sio_GetWriteTimeouts")]
        public static extern int sio_GetWriteTimeouts(int port, ref uint TotalTimeouts);

        [DllImport("PComm.dll", EntryPoint = "sio_SetReadTimeouts")]
        public static extern int sio_SetReadTimeouts(int port, uint TotalTimeouts, uint IntervalTimeouts);

        [DllImport("PComm.dll", EntryPoint = "sio_GetReadTimeouts")]
        public static extern int sio_GetReadTimeouts(int port, ref uint TotalTimeouts, ref uint IntervalTimeouts);

        [DllImport("PComm.dll", EntryPoint = "sio_AbortRead")]
        public static extern int sio_AbortRead(int port);

        [DllImport("PComm.dll", EntryPoint = "sio_ActXoff")]
        public static extern int sio_ActXoff(int port);

        [DllImport("PComm.dll", EntryPoint = "sio_ActXon")]
        public static extern int sio_ActXon(int port);

 //file transfer function phototype 
        public delegate int delegateFileTransfer(long len, int rlen,ref char[] buf, long flen);
        [DllImport("PComm.dll", EntryPoint = "sio_FtASCIITx")]
        public static extern int sio_FtASCIITx(int port, ref char[] fname, delegateFileTransfer FileTransfer, int key);

        [DllImport("PComm.dll", EntryPoint = "sio_FtASCIIRx")]
        public static extern int sio_FtASCIIRx(int port, ref char[] fname, delegateFileTransfer FileTransfer, int key, int sec);

        [DllImport("PComm.dll", EntryPoint = "sio_FtXmodemCheckSumTx")]
        public static extern int sio_FtXmodemCheckSumTx(int port, ref char[] fname, delegateFileTransfer FileTransfer, int key);

        [DllImport("PComm.dll", EntryPoint = "sio_FtXmodemCheckSumRx")]
        public static extern int sio_FtXmodemCheckSumRx(int port, ref char[] fname, delegateFileTransfer FileTransfer, int key);

        [DllImport("PComm.dll", EntryPoint = "sio_FtXmodemCRCTx")]
        public static extern int sio_FtXmodemCRCTx(int port, ref char[] fname, delegateFileTransfer FileTransfer, int key);

        [DllImport("PComm.dll", EntryPoint = "sio_FtXmodemCRCRx")]
        public static extern int sio_FtXmodemCRCRx(int port, ref char[] fname, delegateFileTransfer FileTransfer, int key);

        [DllImport("PComm.dll", EntryPoint = "sio_FtXmodem1KCRCTx")]
        public static extern int sio_FtXmodem1KCRCTx(int port, ref char[] fname, delegateFileTransfer FileTransfer, int key);

        [DllImport("PComm.dll", EntryPoint = "sio_FtXmodem1KCRCRx")]
        public static extern int sio_FtXmodem1KCRCRx(int port, ref char[] fname, delegateFileTransfer FileTransfer, int key);

        [DllImport("PComm.dll", EntryPoint = "sio_FtYmodemTx")]
        public static extern int sio_FtYmodemTx(int port, ref char[] fname, delegateFileTransfer FileTransfer, int key);

        [DllImport("PComm.dll", EntryPoint = "sio_FtYmodemRx")]
        public static extern int sio_FtYmodemRx(int port, ref IntPtr[] ffname, int fno, delegateFileTransfer FileTransfer, int key);

        [DllImport("PComm.dll", EntryPoint = "sio_FtZmodemTx")]
        public static extern int sio_FtZmodemTx(int port, ref char[] fname, delegateFileTransfer FileTransfer, int key);

        [DllImport("PComm.dll", EntryPoint = "sio_FtZmodemRx")]
        public static extern int sio_FtZmodemRx(int port, ref IntPtr[] ffname, int fno, delegateFileTransfer FileTransfer, int key);

        [DllImport("PComm.dll", EntryPoint = "sio_FtKermitTx")]
        public static extern int sio_FtKermitTx(int port, ref char[] fname, delegateFileTransfer FileTransfer, int key);

        [DllImport("PComm.dll", EntryPoint = "sio_FtKermitRx")]
        public static extern int sio_FtKermitRx(int port, ref IntPtr[] ffname, int fno, delegateFileTransfer FileTransfer, int key);

        public const int B50 = 0x00;
        public const int B75 = 0x01;
        public const int B110 = 0x02;
        public const int B134 = 0x03;
        public const int B150 = 0x04;
        public const int B300 = 0x05;
        public const int B600 = 0x06;
        public const int B1200 = 0x07;
        public const int B1800 = 0x08;
        public const int B2400 = 0x09;
        public const int B4800 = 0x0A;
        public const int B7200 = 0x0B;
        public const int B9600 = 0x0C;
        public const int B19200 = 0x0D;
        public const int B38400 = 0x0E;
        public const int B57600 = 0x0F;
        public const int B115200 = 0x10;
        public const int B230400 = 0x11;
        public const int B460800 = 0x12;
        public const int B921600 = 0x13;

        /*	MODE setting		*/
        public const int BIT_5 = 0x00;			/* Word length define	*/
        public const int BIT_6 = 0x01;
        public const int BIT_7 = 0x02;
        public const int BIT_8 = 0x03;

        public const int STOP_1 = 0x00;			/* Stop bits define	*/
        public const int STOP_2 = 0x04;

        public const int P_EVEN = 0x18;			/* Parity define	*/
        public const int P_ODD = 0x08;
        public const int P_SPC = 0x38;
        public const int P_MRK = 0x28;
        public const int P_NONE = 0x00;

        /*	MODEM CONTROL setting	*/
        public const int C_DTR = 0x01;
        public const int C_RTS = 0x02;

        /*	MODEM LINE STATUS	*/
        public const int S_CTS = 0x01;
        public const int S_DSR = 0x02;
        public const int S_RI = 0x04;
        public const int S_CD = 0x08;

        /* error code */
        public const int SIO_OK = 0;
        public const int SIO_BADPORT = -1;/* no such port or port not opened */
        public const int SIO_OUTCONTROL = -2;	/* can't control the board */
        public const int SIO_NODATA = -4;/* no data to read or no buffer to write */
        public const int SIO_OPENFAIL = -5;	/* no such port or port has be opened */
        public const int SIO_RTS_BY_HW = -6;     /* RTS can't set because H/W flowctrl */
        public const int SIO_BADPARM = -7;	/* bad parameter */
        public const int SIO_WIN32FAIL = -8;	/* call win32 function fail, please call */
				        /* GetLastError to get the error code */
        public const int SIO_BOARDNOTSUPPORT = -9;	/* Does not support this board */
        public const int SIO_FAIL = -10;	/* PComm function run result fail */
        public const int SIO_ABORTWRITE = -11;	/* write has blocked, and user abort write */
        public const int SIO_WRITETIMEOUT = -12; /* write timeoue has happened */

        /* file transfer error code */
        public const int SIOFT_OK = 0;
        public const int SIOFT_BADPORT = -1;	/* no such port or port not open */
        public const int SIOFT_TIMEOUT = -2;	/* protocol timeout */
        public const int SIOFT_ABORT = -3;	/* user key abort */
        public const int SIOFT_FUNC = -4;/* func return abort */
        public const int SIOFT_FOPEN = -5;	/* can not open files */
        public const int SIOFT_CANABORT = -6;/* Ymodem CAN signal abort */
        public const int SIOFT_PROTOCOL = -7;	/* Protocol checking error abort */
        public const int SIOFT_SKIP = -8;/* Zmodem remote skip this send file */
        public const int SIOFT_LACKRBUF = -9;/* Zmodem Recv-Buff size must >= 2K bytes */
        public const int SIOFT_WIN32FAIL = -10;	/* OS fail */
				        /* GetLastError to get the error code */
        public const int SIOFT_BOARDNOTSUPPORT = -11;   /* Does not support board */
    }
}
